# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request, FormRequest
import sys
sys.path.append('..')
from movie.items import SchoolClassItem
from scrapy.log import logger
import browsercookie

class SchoolSpider(scrapy.Spider):
    name = 'school'
    allowed_domains = ['www.onlinesjtu.com']
    start_urls = ['http://www.onlinesjtu.com/learningspace/learning/student/dianbolist.asp?userid=717986012051']
    login_urls = 'http://www.onlinesjtu.com/Index.aspx'
    page_url = 'http://www.onlinesjtu.com/learningspace/learning/student/dianbolist.asp?userid=717986012051&realname=&.5585238=.6175615'
    cookies = {'ASPSESSIONIDAQDBBDSA': 'OPIFHLHAIALAIPCNLFIAIBLE'}
    cj = {}

    def parse(self, response):
        print(response.body.decode('gbk'))
        trs = response.xpath('//table[1]/tr').extract()
        if trs:
            for tr in trs[2:]:
                name = tr.xpath('./td[0]/text()').extract()[1]
                url = tr.xpath('./td[6]/text()').extract()[1]
                item = SchoolClassItem()
                item['name'] = name
                item['url'] = url
                yield item
        else:
            logger.error("response error")

    def parsePage(self, response):
        tbody = response.xpath('/html/body/table[2]/tbody/tr')
        logger.info(response.headers.getlist('Set-Cookie'))
        logger.info(response.body.decode('gbk'))
        for tr in tbody[1:]:
            nameNode = tr.xpath('./td[1]/div/text()').extract()
            if not nameNode:
                continue
            name = nameNode[0]
            url = tr.xpath('./td[last()]/div/a/@href').extract_first()
            url = response.urljoin(url)
            print(url)
            header = {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                'Cookie': 'ASPSESSIONIDAQDCCBTA=OPIFHLHAIALAIPCNLFIAIBLE'
            }
            yield Request(url=url, meta={'name': name}, headers=header, callback=self.parseCourse)

    def parseCourse(self, response):
        trs = response.xpath('//table[1]/tbody[1]/tr')
        logger.info(trs)
        for tr in trs:
            url = tr.xpath('./td[1]/div/a[2]/@href').extract_first()
            level = tr.xpath('./td[2]/text()').extract_first()
            url = response.urljoin(url)
            course = {'url': url, 'level': level, 'names' : response.meta['name']}
            logger.info(course)
            yield course

    def start_requests(self):
        header = {
            'Referer': 'http://www.onlinesjtu.com/Index.aspx',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
            'Cookie': 'ASPSESSIONIDAQDCCBTA=OPIFHLHAIALAIPCNLFIAIBLE'
        }
        yield Request(self.page_url, headers=header,  callback=self.parsePage)